<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

require DISCUZ_ROOT.'./source/function/function_mail.php';
require DISCUZ_ROOT.'./source/function/function_date.php';

$email =  $_G['gp_email'];
if(!empty($email)){
	$nick_name =  $_G['gp_nick_name'];
	$birthyear = $_G['gp_birthyear'];
	$birthmonth = $_G['gp_birthmonth'];
	$birthday = $_G['gp_birthday'];
	$prepare_pregnancy = empty($_G['gp_prepare_pregnancy']) ? 0 : 1;

	$data = array('email' => $email, 'nick_name' => $nick_name, 'birthyear' => $birthyear, 'birthmonth' => $birthmonth, 'birthday' => $birthday,
		'prepare_pregnancy' => $prepare_pregnancy, 'update_time' => time(), 'birth_days' => strtotime("$birthyear-$birthmonth-$birthday"),
		'insert_time' => time());

	$verifyurl = $_G['siteurl'].'know.php?mod=confirm';
	$tablename = 'article_subscribe';

	$row_id = DB::insert($tablename, $data, true, false, true);
	if( $row_id > 0 ) {
		$verifyurl .= '&id='.$row_id.'&ut='.$data['update_time'];
		$subscribe_verify_content = lang('know/template', 'subscribe_verify_content',
			array('username' => $email, 'bbname' => '育儿宝典', 'siteurl' => $_G['siteurl'], 'url' => $verifyurl));
		sendmail($email, lang('know/template', 'subscribe_verify_subject'), $subscribe_verify_content);
		showmessage(lang('know/template', 'subscribe_sucess_message'), '', array(), array('alert' => 'right') );
	} else {
		// 已经订阅过，那么，发一封邮件让用户确认是否需要重新订阅。（比如以前输入的宝宝生日有误之类的情况）
		$sql = 'select * from '.DB::table($tablename).' where email = \''.$email.'\'';
		$row = DB::fetch_first($sql);
		if ($row) {
			$url = $_G['siteurl'].'know.php?mod=confirm&id='.$row['id'].'&ut='.$row['update_time'];
			$reurl = $_G['siteurl'].'know.php?mod=resubscribe&id='.$row['id'].'&ut='.$row['update_time'];
			$resubscribe_verify_content = lang('know/template', 'resubscribe_verify_content',
				array('username' => $email, 'bbname' => '育儿宝典', 'siteurl' => $_G['siteurl'], 'url' => $url , 'reurl' => $reurl));
			sendmail($email, lang('know/template', 'resubscribe_verify_subject'), $resubscribe_verify_content);
			showmessage(lang('know/template', 'resubscribe_sucess_message'), '', array(), array('alert' => 'right') );
		}
	}
}
else {
	showmessage(lang('know/template', 'subscribe_email_empty'));
}


?>